package com.qidian.mes.pro.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qidian.mes.pojo.pro.ProProcess;
import com.qidian.mes.pojo.pro.ProRoute;
import com.qidian.mes.pro.mapper.ProProcessMapper;
import com.qidian.mes.pro.mapper.ProRouteMapper;
import com.qidian.mes.pro.service.ProRouteService;
import com.qidian.mes.util.pojo.MESResult;
import org.assertj.core.error.ElementsShouldBeExactly;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.List;

/**
 * @Author 郭靖旖
 * @Company
 * @Time 2023/11/2 14:30
 * @Description
 **/
@Service
public class ProRouteServiceImpl implements ProRouteService {
    @Autowired
    ProRouteMapper proRouteMapper;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Override
    public MESResult selectallByPage(int page, int limit) {
        PageHelper.startPage(page,limit);
        List<ProRoute> list=proRouteMapper.selectallByPage();
        PageInfo<ProRoute> pageInfo=new PageInfo<>(list);
        return MESResult.ok(pageInfo);
    }

    @Override
    public MESResult selectByPage(int page, int limit, ProRoute proRoute) {
        PageHelper.startPage(page,limit);
        List<ProRoute> list=proRouteMapper.selectByPage(proRoute);
        PageInfo<ProRoute> pageInfo=new PageInfo<>(list);
        return MESResult.ok(pageInfo);
    }

    @Override
    public MESResult genRouteCode() {
        String key="ROUTEID";
        StringBuilder id=new StringBuilder();
        id.append("ROUTEORDER");
        Calendar calendar=Calendar.getInstance();
        id.append(calendar.get(Calendar.YEAR)+""+(calendar.get(Calendar.MONTH)+1)+calendar.get(Calendar.DATE));
        Long idTemp=stringRedisTemplate.opsForValue().increment(key);
        String idStr= idTemp.toString();
        if (idStr.length()<5){
            for (int i=0;i<5-idStr.length();i++){
                idStr="0"+idStr;
            }
        }
        id.append(idStr);
        return MESResult.ok(id);
    }

    @Override
    public MESResult save(ProRoute proRoute) {
        ProRoute proRoute1 = new ProRoute();
        proRoute1 = proRouteMapper.selectByRouteName(proRoute);
        if (proRoute1!=null &&proRoute1.getRouteName().equals(proRoute1.getRouteName())){
            return MESResult.duplicate();
        }
        else if (proRouteMapper.save(proRoute)==1)
        {
            return MESResult.ok();
        }
        else
        {
            return MESResult.duplicate();
        }

    }

    @Override
    public MESResult deleteRoute(String routeCode) {
        if (proRouteMapper.deleteRoute(routeCode)==1) {
            return MESResult.ok();
        }else {
            return MESResult.fail();
        }
    }

    @Override
    public MESResult updateProRoute(ProRoute proRoute) {
        ProRoute proRoute1 = new ProRoute();
        proRoute1 = proRouteMapper.selectByRouteName(proRoute);
        if (proRoute1!=null &&proRoute1.getRouteName().equals(proRoute.getRouteName())){
            if (!proRoute1.getRouteId().equals(proRoute.getRouteId()))
            {
                return MESResult.duplicate();
            }
        }
        if (proRouteMapper.updateProcess(proRoute)==1)
        {

            return MESResult.ok();
        }
        else
        {
            return MESResult.fail();
        }
    }
}
